package com.demo.Controller;

import java.util.List;


import javax.ws.rs.DefaultValue;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.demo.entity.Class;
import com.demo.service.ClassService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/Class")
public class ClassController {
	@Autowired
	private ClassService classService;
	
	@RequestMapping(value="/addClass")
	public String addClass(Class classInfo) throws Exception{
		int addClass = classService.addClass(classInfo);
		if(addClass>0)
			return "ok";
		else 
			return "error";
	}
	
	@RequestMapping(value="/deleteClass/{id}")
	public String deleteClass(@PathVariable String id)throws Exception{
		int deleteClassById = classService.deleteClassById(id);
		if(deleteClassById>0)
			return "ok";
		else 
			return "error";
	}
	
	@RequestMapping(value="/updateClassById")
	public String updateClassById( Class classInfo)throws Exception{
		int updateClass = classService.updateClass(classInfo);
		if(updateClass>0)
			return "ok";
		else 
			return "error";
	}
	
	@RequestMapping(value="/findClassById/{id}")
	public Class findClassById(@PathVariable String id)throws Exception{
		return classService.selectClassById(id);
	}
	
	@RequestMapping(value="/findAllClass")
	public PageInfo<Class> findAllClass(Class classInfo ,@DefaultValue("1") Integer pageNum ,@DefaultValue("5") Integer pageSize) throws Exception{
		PageHelper.startPage(pageNum, pageSize);
		classInfo.setId(classInfo.getId().trim());
		classInfo.setClassname(classInfo.getClassname().trim());
		List<Class> selectAllClass = classService.selectAllClass(classInfo);
		for (Class class1 : selectAllClass) {
			System.out.println(class1);
		}
		PageInfo<Class> pageInfo = new PageInfo<Class>(selectAllClass);
		return pageInfo;
	}
	@RequestMapping(value="/findAllClassDistinct")
	public List<Class> findAllClassDistinct() throws Exception{
		return classService.selectAllClassDistinct();
	}
}
